package com.fingard.dsp.bank.directbank.zyb02;

import java.util.HashMap;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IQuoteCheck;
import com.fingard.dsp.bank.reqrespobj.quotecheck.ReqAQ01BalQry;
import com.fingard.dsp.bank.reqrespobj.quotecheck.ReqAtsAQ01BalQry;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ZYB02QuoteCheck extends ZYB02Base implements IQuoteCheck {
    @Override
    public ReqAtsAQ01BalQry d_AQ01_BalQry(ReqAQ01BalQry p_reqAQ01) throws Exception {
        if("001".equals(p_reqAQ01.reqHeader.commandCode)) {//��ѯ���ϵͳ�Ƿ���
            return isPaymentSystemWork(p_reqAQ01);
        } else if("002".equals(p_reqAQ01.reqHeader.commandCode)) {//��ѯ��ǰʱ�佻���޶�
            return queryLimitAmount(p_reqAQ01);
        } else {//������ʲô���߰���Ĳ�ѯ�����if
            return null;
        }
    }

    /**
     * ��ѯ���֧��ϵͳ�Ƿ���
     * @param p_reqElse
     * @return
     * @throws Exception
     */
    private ReqAtsAQ01BalQry isPaymentSystemWork(ReqAQ01BalQry p_reqElse) throws Exception {
        String seqNumberStart = getBankConfig().getMapValueByKey("Sys_Head","CONSUMER_ID")+reqHeader.corporation;
        ReqAtsAQ01BalQry retAts = new ReqAtsAQ01BalQry(p_reqElse.reqHeader);
        HashMap<String,String> result = new HashMap<String,String>();
//        retAts.writeStart();

        //����״̬LOGIN_STATUSֵ��3����ǰϵͳ״̬STATUS��10�� ��ʾ�����������ǹر�
//        result.put("RESPCODE","-1");//saas��dsp��ͨѶ��־
        result.put("LOGIN_STATUS","-1");//����״̬
        result.put("STATUS","-1");//��ǰϵͳ״̬
//        result.put("DESC","");//�ɹ�����ʧ�ܵ�������Ϣ
        //д����ATS��ͳһͷ����Ϣ
        retAts.writeStart();

        //��ֵͨ�ò���
//        AQ01Header bankHeader = new AQ01Header();
        ZYB02Header bankHeader = new ZYB02Header();
        bankHeader.SEQ_NO = seqNumberStart+reqHeader.transSeqID;
        bankHeader.TRAN_CODE = getBankConfig().getMapValueByKey("TRANS_CODE", "QUERYBANKISWORKING");
        String transTime = p_reqElse.reqHeader.transTime;
        bankHeader.TRAN_DATE = Format.Date8Format.format(DateHelper.convertToDateExtra(transTime));
        bankHeader.TRAN_TIMESTAMP = Format.Time9Format.format(DateHelper.convertToDateExtra(transTime));
        bankHeader.USER_IP = getIpInfoMap(p_reqElse.getOriReqXml()).get("userIP");
        bankHeader.SERVER_IP = getIpInfoMap(p_reqElse.getOriReqXml()).get("serverIP");
        //�����б��ĵ�XML����
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.autoFormat = false;
        //д��ͨ�õ�ͷ���ڵ�
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.insertIntoTag("CURRENT_NUM", "-1");
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

            if (tmpBkReader != null) {
                String ret_status = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_STATUS");
                String ret_code = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_ARRAY/Row/RET_CODE");
                String ret_msg = tmpBkReader.getFirstText("/Reply_Msg/Sys_Head/RET_ARRAY/Row/RET_MSG");
                if(TMPSTATUS.equals(ret_status) && RET_CODE.equals(ret_code)) {
                    while (tmpBkReader.read("/Reply_Msg/Body/SYS_STATUS/Row")) {
                        String format = tmpBkReader.readedXElement.getSingleText("FORMAT");
                        String login_status = tmpBkReader.readedXElement.getSingleText("LOGIN_STATUS");
                        String status = tmpBkReader.readedXElement.getSingleText("STATUS");
                        if("HVPS".equals(format)) {
                            result.put("RESPCODE","0");
                            result.put("LOGIN_STATUS",login_status);
                            result.put("STATUS",status);
                            break;
                        }
                    }
                } else {
                    result.put("DESC",ret_status+"-"+ret_code+"-"+ret_msg);
                }
            } else {
                result.put("DESC",tmpSbMsg.toString());
            }
        } else {
            result.put("DESC",tmpStrRet[1]);
        }

        retAts.writeRespCode(result.get("RESPCODE"));
        retAts.writeRespInfo(result.get("DESC"));
        XmlTextWriter retXmlWriter = retAts.retXmlWriter;
        retXmlWriter.writeElementString("LOGINSTATUS", result.get("LOGIN_STATUS"));
        retXmlWriter.writeElementString("SYSTEMSTATUS",result.get("STATUS"));
        retAts.writeEnd();
        return retAts;
    }

    /**
     * ��ѯ��ǰʱ�����д�С��ϵͳ�Ľ����޶Ŀǰֻ�ǲ�ѯ��ǰʱ��С��ϵͳ�Ľ����޶�(������С�ڵ���5�򣬽ڼ���С�ڵ���50��)
     * @param p_reqElse
     * @return
     * @throws Exception
     */
    private ReqAtsAQ01BalQry queryLimitAmount(ReqAQ01BalQry p_reqElse) throws Exception {
        /**
         * ent.MCMbcTrsActLimitQry  ����ͨ��ѯ�޶�
         * ��־��(��־��) 11-27 17:43:55
         * SuperBankLimitʵʱ�������,LittleLimit��ͨ�������
         * <SuperBankLimit>60000</SuperBankLimit>
         * <LittleLimit>510000</LittleLimit>
         * ��־��(��־��) 11-27 17:44:08
         * ��ĿǰֻȡLittleLimit�ֶ�
         */
        String seqNumberStart = getBankConfig().getMapValueByKey("Sys_Head","CONSUMER_ID")+reqHeader.corporation;
        ReqAtsAQ01BalQry retAts = new ReqAtsAQ01BalQry(p_reqElse.reqHeader);
        HashMap<String,String> result = new HashMap<String,String>();
//        retAts.writeStart();

//        result.put("RESPCODE","-1");//saas��dsp��ͨѶ��־
        result.put("LITTLELIMIT","-1");//��ǰʱ��С��ϵͳ���޶�
        result.put("DESC","");//�ɹ�����ʧ�ܵ�������Ϣ
//        retAts.retHashMap = result;
        //д����ATS��ͳһͷ����Ϣ
        retAts.writeStart();

        //��ֵͨ�ò���
//        AQ01Header bankHeader = new AQ01Header();
        ZYB02Header bankHeader = new ZYB02Header();
        bankHeader.SEQ_NO = seqNumberStart + reqHeader.transSeqID;
        bankHeader.TRAN_CODE = getBankConfig().getMapValueByKey("TRANS_CODE", "QUERYLIMITAMOUNT");
        String transTime = p_reqElse.reqHeader.transTime;
        bankHeader.TRAN_DATE = Format.Date10Format.format(DateHelper.convertToDateExtra(transTime));
        bankHeader.TRAN_TIMESTAMP = TIME12FORMAT.format(DateHelper.convertToDateExtra(transTime));
        bankHeader.USER_IP = getIpInfoMap(p_reqElse.getOriReqXml()).get("userIP");
        bankHeader.SERVER_IP = getIpInfoMap(p_reqElse.getOriReqXml()).get("serverIP");
        //�����б��ĵ�XML����
        XmlTextWriter xmlWriter = new XmlTextWriter();
        xmlWriter.autoFormat = false;
        //д��ͨ�õ�ͷ���ڵ�
        writeCommon(xmlWriter, bankHeader);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendXmlToBank(xmlWriter.toXmlString());
        /**
         * <?xml version="1.0" encoding="UTF-8"?>
         * <Message>
         * <Head>
         * <_MCSJnlNo>52517796</_MCSJnlNo>
         * <_ResponseSystemId>MCA</_ResponseSystemId>
         * <_RejCode>000000</_RejCode>
         * <_RejMsg></_RejMsg>
         * <_MCHJnlNo>1936502</_MCHJnlNo>
         * <_TransactionId>ent.MCMbcTrsActLimitQry</_TransactionId>
         * <_MCHTimestamp>2017-07-20 18:41:01.612</_MCHTimestamp>
         * </Head>
         * <Body>
         * <SuperBankLimit>50000</SuperBankLimit>
         * <LittleLimit>50000</LittleLimit>
         * </Body>
         * </Message>
         */
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

            if (tmpBkReader != null) {
                String ret_code = tmpBkReader.getFirstText("/Message/Head/_RejCode");
                String ret_msg = tmpBkReader.getFirstText("/Message/Head/_RejMsg");
                if(RET_CODE.equals(ret_code)) {
                    String littleLimit = tmpBkReader.getFirstText("/Message/Body/LittleLimit");
                    result.put("RESPCODE","0");
                    result.put("LITTLELIMIT",littleLimit);
                    result.put("DESC","��ѯ�޶�ɹ���");
                } else {
                    result.put("DESC",ret_code+"-"+ret_msg);
                }
            } else {
                result.put("DESC",tmpSbMsg.toString());
            }
        } else {
            result.put("DESC",tmpStrRet[1]);
        }

        retAts.writeRespCode(result.get("RESPCODE"));
        retAts.writeRespInfo(result.get("DESC"));
        XmlTextWriter retXmlWriter = retAts.retXmlWriter;
        retXmlWriter.writeElementString("LITTLELIMIT", result.get("LITTLELIMIT"));
        retAts.writeEnd();
        return retAts;
    }
}
